{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from pycocotools.coco import COCO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_root = 'OilChangeDataset'\n",
    "ann_file = '20171103_OilChange.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.46s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "coco = COCO(os.path.join(data_root, 'annotations', ann_file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['info', 'cameras', 'licenses', 'images', 'annotations', 'categories'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coco.dataset.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id: 0 type: kinect2\n",
      "id: 1 type: pr2_kinect\n",
      "id: 2 type: momap_kinect\n",
      "id: 3 type: floating_kinect1\n",
      "id: 4 type: xtion\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(coco.dataset['cameras'])):\n",
    "    camera = coco.dataset['cameras'][i]\n",
    "    print('id: {} type: {}'.format(camera['id'], camera['name']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'supercategory': 'objects',\n",
       "  'mesh': 'oil_bottle.stl',\n",
       "  'id': 1,\n",
       "  'name': 'oil_bottle'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'power_steering_bottle.stl',\n",
       "  'id': 2,\n",
       "  'name': 'fluid_bottle'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'oil_filter.stl',\n",
       "  'id': 3,\n",
       "  'name': 'oilfilter'},\n",
       " {'supercategory': 'objects', 'mesh': 'funnel.stl', 'id': 4, 'name': 'funnel'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'engine_high_quality.stl',\n",
       "  'id': 5,\n",
       "  'name': 'engine'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'blue_funnel.stl',\n",
       "  'id': 6,\n",
       "  'name': 'blue_funnel'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'tissue_box.stl',\n",
       "  'id': 7,\n",
       "  'name': 'tissue_box'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'drill_decimated_mesh.stl',\n",
       "  'id': 8,\n",
       "  'name': 'drill'},\n",
       " {'supercategory': 'objects',\n",
       "  'mesh': 'cracker_box.stl',\n",
       "  'id': 9,\n",
       "  'name': 'cracker_box'},\n",
       " {'supercategory': 'objects', 'mesh': 'spam.stl', 'id': 10, 'name': 'spam'}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coco.dataset['categories']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9829"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(coco.getImgIds())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9829"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(coco.dataset['images'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.49s)\n",
      "creating index...\n",
      "index created!\n",
      "loading annotations into memory...\n",
      "Done (t=0.15s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "train_coco = COCO(os.path.join(data_root, 'annotations', 'train_' + ann_file))\n",
    "val_coco = COCO(os.path.join(data_root, 'annotations', 'val_' + ann_file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7879, 1950)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_coco.getImgIds()), len(val_coco.getImgIds())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(229, 683)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "camera_name = 'floating_kinect1'\n",
    "cameras = {camera['name']: camera for camera in val_coco.dataset['cameras']}\n",
    "camera_id = cameras[camera_name]['id']\n",
    "imgs = val_coco.loadImgs(val_coco.getImgIds())\n",
    "def camera_filter(camera_id):\n",
    "    return lambda x: x['camera_id'] == camera_id\n",
    "img_ids = [img['id'] for img in filter(camera_filter(camera_id), imgs)]\n",
    "object_instances = val_coco.loadAnns(val_coco.getAnnIds(imgIds=img_ids))\n",
    "len(img_ids), len(object_instances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
